<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<script type="text/javascript">
    require([ "dojo/_base/array", "dojo/_base/xhr", "dojo/request", "dojo/topic", "dojo/json" ],
            function(array, xhr, request, topic, JSON) {
                var deletItemsCount = 0;
                appointmentRestForm = function(formId) {
                    dijit.byId(formId).reset();
                    dijit.byId("AppointmentContent").setValue("");
                    if (formId.indexOf('UFD') != -1) {
                        var rowid = jQuery("#appointmentGird").jqGrid('getGridParam', 'selrow');
                        var appointment = jQuery("#appointmentGird").jqGrid('getRowData', rowid);
                        dijit.byId('appointmentUFD').setValues(appointment);
                    }
                };
                topic.subscribe("appointment/insertOrUpdate", appointmentRestForm);
                var syncDB = function(url, jsonData) {
                    request.post(url, {
                        data : jsonData,
                        headers : {
                            'Content-Type' : "application/json; charset=utf-8"
                        },
                        timeout : 30000
                    }).then(function(response) {
                        topic.publish("notifyMsgTopic", {
                            message : response,
                            type : "message",
                            duration : 1000
                        });
                        var currentPage = jQuery('#appointmentGird').jqGrid('getGridParam', 'page');
                        var pageSize = jQuery('#appointmentGird').jqGrid('getGridParam', 'rowNum');
                        var totalCount = jQuery('#appointmentGird').jqGrid('getGridParam', 'records') - deletItemsCount;
                        var totalPages = Math.ceil(totalCount / pageSize);
                        jQuery("#appointmentGird").jqGrid('setGridParam', {
                            page : currentPage > totalPages ? totalPages : currentPage
                        }).trigger("reloadGrid");
                    }, function(error) {
                        alert(error);
                    });
                };
                appointmentSubmitForm = function(formId, type) {
                    var url = "appointment/insert";
                    if (type == 'u') {
                        url = "appointment/update";
                    }
                    var form = dijit.byId(formId);
                    if (form.isValid()) {
                        var json = JSON.stringify(form.getValues());
                        syncDB(url, json);
                        form.hide();
                        topic.publish("appointment/insertOrUpdate", formId);
                    } else {
                        form.validate();
                    }
                };
                appointmentDelete = function() {
                    var ids = jQuery("#appointmentGird").jqGrid('getGridParam', 'selarrrow') || [];
                    deletItemsCount = ids.length;
                    if (ids.length > 0) {
                        if (confirm('确认删除所选定的项？')) {
                            var appointment = {};
                            appointment.id = '';
                            array.forEach(ids, function(entry, i) {
                                appointment.id += entry + ",";
                            });
                            syncDB("appointment/delete", JSON.stringify(appointments));
                        }
                    } else {
                        alert('请选择至少一条数据!');
                    }
                };
                appointmentQuery = function(formId) {
                    var appointment = dijit.byId(formId).getValues();
                    jQuery("#appointmentGird").jqGrid('setGridParam', {
                        postData : appointment,
                        mtype : "post"
                    }).trigger('reloadGrid');
                };
            });
</script>
<div data-dojo-type="dijit/Dialog" id="appointmentIFD" name="appointmentIFD" title="新增">
  <div class="dijitDialogPaneContentArea">
    <table>
      <tr>
        <td><label for="subject">主题: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="subject"></td>
      </tr>
      <tr>
        <td><label for="starttime">开始时间: </label></td>
        <td>
            <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="starttime" id="InsertStartTime" class="Wdate" onClick="WdatePicker({maxDate:'#F{$dp.$D(\'InsertEndTime\')}',dateFmt:'yyyy-MM-dd HH:mm:ss'})" >           
        </td>
      </tr>
      <tr>
        <td><label for="endtime">结束时间: </label></td>
        <td>
            <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="endtime" id="InsertEndTime" class="Wdate" onClick="WdatePicker({minDate:'#F{$dp.$D(\'InsertStartTime\')}',dateFmt:'yyyy-MM-dd HH:mm:ss'})" >           
        </td>
      </tr>
      <tr>
        <td><label for="content">约会内容: </label></td>
        <td>
            <div name="content" id="AppointmentContent" data-dojo-type="dijit/Editor" data-dojo-props="plugins:['bold','italic','|','cut','copy','paste','|','insertUnorderedList']"></div>
        </td>
      </tr>
      <tr>
        <td><label for="notes">备注: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="notes"></td>
      </tr>
      <tr>
        <td><label for="location">地点: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="location"></td>
      </tr>
      <tr>
        <td><label for="inviteemails">受邀人邮箱: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="regExp: '^\\w*?@\\w*?\.(com|net|cn|org|com\.cn)$', invalidMessage:'请输入正确邮箱地址'" type="text"
          name="inviteemails"></td>
      </tr> 
      <tr>
        <td>
            <label for="sendmessage">是否发送短信提醒: </label>
        </td>
        <td>
            <select data-dojo-type="dijit/form/Select" name="sendmessage" style="width: 100px;">
            <option value="0">否</option>
            <option value="1">是</option>
            </select>
            <label for="sendmail">是否发送邮件提醒: </label>
            <select data-dojo-type="dijit/form/Select" name="sendmail" style="width: 100px;">
            <option value="0">否</option>
            <option value="1">是</option>
            </select>
        </td> 
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="appointmentSubmitForm('appointmentIFD','i')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="appointmentRestForm('appointmentIFD');">重置</button>
  </div>
</div>
<div data-dojo-type="dijit/Dialog" id="appointmentUFD" title="更新">
  <div class="dijitDialogPaneContentArea">
    <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, readOnly: true" type="text"
      name="appointid" style="display: none;">
        <table>
      <tr>
        <td><label for="subject">主题: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="subject"></td>
      </tr>
      <tr>
        <td><label for="starttime">开始时间: </label></td>
        <td>
            <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="starttime" id="UpdateStartTime" class="Wdate" onClick="WdatePicker({maxDate:'#F{$dp.$D(\'UpdateEndTime\')}',dateFmt:'yyyy-MM-dd HH:mm:ss'})" >           
        </td>
      </tr>
      <tr>
        <td><label for="endtime">结束时间: </label></td>
        <td>
            <input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true" type="text" name="endtime" id="UpdateEndTime" class="Wdate" onClick="WdatePicker({minDate:'#F{$dp.$D(\'UpdateStartTime\')}',dateFmt:'yyyy-MM-dd HH:mm:ss'})" >           
        </td>
      </tr>
      <tr>
        <td><label for="content">约会内容: </label></td>
        <td>
            <div name="content" data-dojo-type="dijit/Editor" data-dojo-props="plugins:['bold','italic','|','cut','copy','paste','|','insertUnorderedList']"></div>
        </td>
      </tr>
      <tr>
        <td><label for="notes">备注: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="notes"></td>
      </tr>
      <tr>
        <td><label for="location">地点: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="required: true, trim:true" type="text"
          name="location"></td>
      </tr>
      <tr>
        <td><label for="inviteemails">受邀人邮箱: </label></td>
        <td><input data-dojo-type="dijit/form/ValidationTextBox" data-dojo-props="regExp: '^\\w*?@\\w*?\.(com|net|cn|org|com\.cn)$', invalidMessage:'请输入正确邮箱地址'" type="text"
          name="inviteemails"></td>
      </tr> 
      <tr>
        <td>
            <label for="sendmessage">是否发送短信提醒: </label>
        </td>
        <td>
            <select data-dojo-type="dijit/form/Select" name="sendmessage" style="width: 100px;">
            <option value="0">否</option>
            <option value="1">是</option>
            </select>
            <label for="sendmail">是否发送邮件提醒: </label>
            <select data-dojo-type="dijit/form/Select" name="sendmail" style="width: 100px;">
            <option value="0">否</option>
            <option value="1">是</option>
            </select>
        </td> 
      </tr>
    </table>
  </div>
  <div class="dijitDialogPaneActionBar">
    <button data-dojo-type="dijit/form/Button" type="button" onclick="appointmentSubmitForm('appointmentUFD','u')">保存</button>
    <button data-dojo-type="dijit/form/Button" type="button" onclick="appointmentRestForm('appointmentUFD');">重置</button>
  </div>
</div>
<fieldset>
  <legend>&nbsp;&nbsp;信息选项&nbsp;&nbsp;</legend>
  <form id="queryAppointmentForm" data-dojo-type="dijit/form/Form" method="post">
    <div style="margin-left: 5px;">
      <table>
        <tr>
          <td><label for="starttime">开始时间: </label></td>
          <td width="200"><input style="width: 180px;" data-dojo-type="dijit/form/ValidationTextBox" name="starttime" class="Wdate" id="Astarttime" onClick="WdatePicker({maxDate:'#F{$dp.$D(\'Aendtime\')}',dateFmt:'yyyy-MM-dd HH:mm:ss'})"></td>
          <td><label for="endtime">结束时间: </label></td>
          <td width="200"><input style="width: 180px;" data-dojo-type="dijit/form/ValidationTextBox" name="endtime" class="Wdate" id="Aendtime" onClick="WdatePicker({minDate:'#F{$dp.$D(\'Astarttime\')}',dateFmt:'yyyy-MM-dd HH:mm:ss'})"></td>         
        </tr>
        <tr>
          <td><label for="content">约会内容: </label></td>
          <td width="200"><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="content"></td>
          <td><label for="subject">主题: </label></td>
          <td width="200"><input data-dojo-type="dijit/form/TextBox" data-dojo-props="trim: true" type="text" name="subject"></td>
        </tr>
      </table>
    </div>
  </form>
  <div style="padding: 5px;">
    <button id="appointmentInsertBtn" data-dojo-type="dijit/form/Button" type="submit" label="新增">
      <script type="dojo/method" event="onClick" args="evt">
        dijit.byId('appointmentIFD').show();
      </script>
    </button>
    <button id="appointmentDeleteBtn" data-dojo-type="dijit/form/Button" type="button" label="删除" onclick="appointmentDelete();"></button>
    <button id="appointmentUpdateBtn" data-dojo-type="dijit/form/Button" type="button" label="更新">
      <script type="dojo/method" event="onClick" args="evt">
        var ids = jQuery("#appointmentGird").jqGrid('getGridParam', 'selarrrow') || [];
        if (ids.length != 1) {
          alert('每次只能修改一条数据!');
        }else{
          var appointment = jQuery("#appointmentGird").jqGrid('getRowData', ids[0]);
          dijit.byId('appointmentUFD').setValues(appointment);
          dijit.byId('appointmentUFD').show();
        }
      </script>
    </button>
    <button id="appointmentQueryBtn" data-dojo-type="dijit/form/Button" type="button" label="查询"
      onclick="appointmentQuery('queryAppointmentForm');"></button>
  </div>
</fieldset>
<div id="gird">
  <table id="appointmentGird"></table>
  <div id="appointmentToolbar"></div>
</div>
<script type="text/javascript">
    jQuery("#appointmentGird").jqGrid({
        url : 'appointment/queryAsList',
        datatype : "json",
        autowidth : true,
        rownumbers : true,
        height : 'auto',
        colNames : [
          '约会ID',
          '用户ID',
          '主题',
          '开始时间',
          '结束时间',
          '约会内容',
          '备注',
          '地点',
          '受邀人邮箱',
          '是否发送短信提醒',
          '是否发送邮件提醒'
        ],
        colModel : [ 
        {
          name : 'appointid',
          index : 'appointid',
          width : 60,
          key : true,
          hidden : true,
          align : 'center'
        },
        {
          name : 'userid',
          index : 'userid',
          hidden : true      
        },
        {
          name : 'subject',
          index : 'subject'
        },
        {
          name : 'starttime',
          index : 'starttime'
        },
        {
          name : 'endtime',
          index : 'endtime'
        },
        {
          name : 'content',
          index : 'content'
        },
        {
          name : 'notes',
          index : 'notes',
          hidden : true
        },
        {
          name : 'location',
          index : 'location'
        },
        {
          name : 'inviteemails',
          index : 'inviteemails'
        },
        {
          name : 'sendmessage',
          index : 'sendmessage',
          hidden : true
        },
        {
          name : 'sendmail',
          index : 'sendmail',
          hidden : true
        }
        ],
        rowNum : 15,
        rowList : [ 15, 20, 25, 30 ],
        pager : '#appointmentToolbar',
        // sortname : 'username',
        recordpos : 'right',
        viewrecords : true,
        sortorder : "desc",
        jsonReader : {
            repeatitems : false
        },
        multiselect : true,
        shrinkToFit : false,
        caption : "我的约会列表"
    });
    jQuery("#appointmentGird").jqGrid('navGrid', '#appointmentToolbar', {
        edit : false,
        add : false,
        del : false,
        search : false,
        refresh : true
    });
</script>
